package gui.client;



import gui.Login.MessageCenter;

import javax.swing.JPanel;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


import javax.swing.border.TitledBorder;

import control.LoginController;

import system.SystemTypes;
import system.VCP;
/**
 * this class use as the main menu for the client 
 * @author G8
 *
 */
public class ClientGUI extends JPanel implements ActionListener
{
	private JButton btnLogout;
	private JButton btnAdvanceOrder;
	private JButton btnCloseOrder;
	private JButton btnExistingOrder;
	private JButton btnSubscribe;
	private JButton btnOccasionalCustomer;
	public JButton btnBack;
	private AdvanceOrderScreen AdvanceOrderScreen;
	private CloseOrderScreen closeOrderScreen;
	private TrackOrdeScreen trackOrdeScreen;
	private SubscriptionScreen subscriptionScreen;
	private OccasionalCustomerOrderScreen occasionalCustomerOrderScreen;
	private JLabel lblDateTime;
	private JButton btnMessage;
	private MessageCenter messageCenter;
	private JButton btnComplaint;
	private CreateComplaintScreen createComplaintScreen;
	private JButton btnBack_1;
	private JButton btnSubRenew;
	private SubscriptionRenewalScreen subRenewScreen;
	private SubscribedClientScreen subClientScreen;
	private JButton btnSubscribed;
	
	/**
	 * Client GUI Screen Constructor 
	 */
	public ClientGUI()
	{
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize() 
	{
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Client Menu", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		//Add buttons:
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		
		btnAdvanceOrder = new JButton("Order In Advance");
		btnAdvanceOrder.setBounds(20, 53, 207, 46);
		btnAdvanceOrder.addActionListener(this);
		add(btnAdvanceOrder);
		
		btnCloseOrder = new JButton("Close Order");
		btnCloseOrder.setBounds(264, 167, 207, 46);
		btnCloseOrder.addActionListener(this);
		add(btnCloseOrder);
		
		btnExistingOrder = new JButton("Existing Order");
		btnExistingOrder.setBounds(20, 224, 207, 46);
		btnExistingOrder.addActionListener(this);
		add(btnExistingOrder);
		
		btnSubscribe = new JButton("Subscribe");
		btnSubscribe.setBounds(20, 167, 207, 46);
		btnSubscribe.addActionListener(this);
		add(btnSubscribe);
		
		btnOccasionalCustomer = new JButton("Occasional Order");
		btnOccasionalCustomer.setBounds(20, 110, 207, 46);
		btnOccasionalCustomer.addActionListener(this);
		add(btnOccasionalCustomer);
		
		btnMessage = new JButton("Message Center");
		btnMessage.setBounds(264, 110, 207, 46);
		btnMessage.addActionListener(this);
		add(btnMessage);
		
		btnComplaint = new JButton("Complaint");
		btnComplaint.setBounds(20, 281, 207, 46);
		btnComplaint.addActionListener(this);
		add(btnComplaint);
		
		lblDateTime = new JLabel("Date and Time");
		lblDateTime.setBounds(201, 332, 97, 14);
		lblDateTime.setText(VCP.getDateTime());
		add(lblDateTime);
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(AdvanceOrderScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		btnBack.setVisible(false);
		if (VCP.currentUser.getPrivilege()!=5)
			btnBack.setVisible(true);
		add(btnBack);
		
		btnSubRenew = new JButton("Subscription Renewal");
		btnSubRenew.setBounds(264, 224, 207, 46);
		btnSubRenew.addActionListener(this);
		add(btnSubRenew);
		
		btnSubscribed = new JButton("Subscribed Clients");
		btnSubscribed.setBounds(264, 281, 207, 46);
		btnSubscribed.addActionListener(this);
		add(btnSubscribed);
	}
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnBack){
			VCP.setGUI(VCP.currentUser.getPrivilege(),this);
		}
		if (e.getSource() == btnMessage){
			try{
				messageCenter = new MessageCenter();
				VCP.setScreen(this, messageCenter);
			}
			catch(Exception ae){
				JOptionPane.showMessageDialog(null, "No new messages !", "Attention",
	                    JOptionPane.INFORMATION_MESSAGE);
			}
		}
		if (e.getSource() == btnSubscribed){
			subClientScreen = new SubscribedClientScreen();
			VCP.setScreen(this, subClientScreen);
		}
		if (e.getSource() == btnSubRenew){
			subRenewScreen = new SubscriptionRenewalScreen();
			VCP.setScreen(this, subRenewScreen);
		}
		if (e.getSource() == btnAdvanceOrder){
			AdvanceOrderScreen = new AdvanceOrderScreen();
			VCP.setScreen(this, AdvanceOrderScreen);
		}
		if (e.getSource() == btnCloseOrder){
			closeOrderScreen = new CloseOrderScreen();
			VCP.setScreen(this, closeOrderScreen);
		}
		if (e.getSource() == btnExistingOrder){
			trackOrdeScreen = new TrackOrdeScreen();
			VCP.setScreen(this, trackOrdeScreen);
		}
		if (e.getSource() == btnOccasionalCustomer){
			occasionalCustomerOrderScreen = new OccasionalCustomerOrderScreen();
			VCP.setScreen(this, occasionalCustomerOrderScreen);
		}
		if (e.getSource() == btnSubscribe){
			subscriptionScreen = new SubscriptionScreen();
			VCP.setScreen(this, subscriptionScreen);
		}
		if (e.getSource() == btnComplaint){
			createComplaintScreen = new CreateComplaintScreen();
			VCP.setScreen(this, createComplaintScreen);
		}
		if (e.getSource() == btnLogout){
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}	
	}
}
